package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.crossorewar.CrossOreStationInfo;
import com.yanqu.road.entity.enums.activity.eActivityOreWarType;
import com.yanqu.road.entity.orewar.OrePitInfo;
import com.yanqu.road.entity.orewar.OreStationInfo;
import com.yanqu.road.pb.common.CommonProto;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.pb.orewaractivity.OreWarActivityProto;
import com.yanqu.road.server.logic.activity.orewar.OreWarActivityData;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.orewar.CrossOreWar;
import com.yanqu.road.server.manger.activity.orewar.OreWarMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class OreWarActivityPb {

    private static OreWarActivityProto.OreWarActivityTempMsg.Builder parseOreWarActivityTempMsg(ActivityInfo activityInfo, String language){
        if(null == activityInfo){
            return null;
        }
        OreWarActivityProto.OreWarActivityTempMsg.Builder msg = OreWarActivityProto.OreWarActivityTempMsg.newBuilder();
        msg.setActivityId(activityInfo.getActivityId());
        msg.setChildType(activityInfo.getChildType());
        msg.setActivityName(ServerLanguageMgr.getContent(activityInfo.getActivityName(), language));
        msg.setDescription(ServerLanguageMgr.getContent(activityInfo.getActivityDesc(), language));
        msg.setBeginShowTime(activityInfo.getBeginShowTime());
        msg.setEndShowTime(activityInfo.getEndShowTime());
        msg.setBeginTime(activityInfo.getBeginTime());
        msg.setEndTime(activityInfo.getEndTime());
        msg.setIcon(activityInfo.getParam2());
        msg.setPic(activityInfo.getParam3());
        msg.setParam4(activityInfo.getParam4());
        msg.setParam5(activityInfo.getParam5());
        msg.setParam6(activityInfo.getParam6());
        msg.addAllServerList(activityInfo.getServerIdList());
        msg.setUnionGroup(activityInfo.getUnionGroup());
        return msg;
    }

    public static OreWarActivityProto.OreWarActivityMsg.Builder parseOreWarActivityMsg(long userId,ActivityInfo activityInfo,
            Map<Integer, List<ActivityConditionInfo>> activityConditionInfoMap, Map<Integer, MallInfo> activityMallInfoMap, String language){
        if(null == activityInfo) {
            return null;
        }
        OreWarActivityProto.OreWarActivityMsg.Builder resultMsg = OreWarActivityProto.OreWarActivityMsg.newBuilder();
        //活动
        OreWarActivityProto.OreWarActivityTempMsg.Builder activityMsg = parseOreWarActivityTempMsg(activityInfo, language);
        if (null != activityMsg) {
            resultMsg.setActivityInfo(activityMsg);
        }
        //活动条件
        if(null != activityConditionInfoMap) {
            for (List<ActivityConditionInfo> conditionInfoList : activityConditionInfoMap.values()) {
                for (ActivityConditionInfo conditionInfo : conditionInfoList) {
                    CommonActivityProto.CommonActivityConditionTempMsg.Builder conditionMsg = CommonActivityPb.parseCommonActivityConditionTempMsg(conditionInfo, language);
                    if (null != conditionMsg) {
                        resultMsg.addActivityConditionInfoList(conditionMsg);
                    }
                }
            }
        }
        //活动商品
        if(null != activityMallInfoMap) {
            for (MallInfo mallInfo : activityMallInfoMap.values()) {
                CommonProto.MallTempMsg.Builder mallMsg = CommonPb.parseMallTempMsg(mallInfo, language);
                if (null != mallMsg) {
                    resultMsg.addActivityMallInfoList(mallMsg);
                }
            }
        }
        //活动坑位
        if(activityInfo.getChildType() == eActivityOreWarType.OreWar.getChildType()){
            Map<Integer, OreStationInfo> oreStationMap = OreWarMgr.getOreStationMap(activityInfo.getActivityId());
            for(OreStationInfo oreStationInfo : oreStationMap.values()){
                OreWarActivityProto.OreWarActivityPitTemp.Builder temp = OreWarActivityProto.OreWarActivityPitTemp.newBuilder();
                temp.setId(oreStationInfo.getStationId());
                temp.setName(oreStationInfo.getStationName());
                temp.setIcon(oreStationInfo.getIcon());
                temp.setDescNum(oreStationInfo.getShowStationIndex());
                temp.setPos(oreStationInfo.getPosition());
                temp.setCapacity(oreStationInfo.getPitCount());
                temp.setOutputNum(oreStationInfo.getOutputSpeedAddition());
                temp.setOutNumAdd(oreStationInfo.getAddOutputSpeedAddition());
                temp.setIsNotice(oreStationInfo.isNotice());
                resultMsg.addPitList(temp);
            }
        }else if(activityInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()){
            int groupId = GameUnionActivityGroupMgr.getGroupId(activityInfo.getActivityId(), userId);
            Map<Integer, CrossOreStationInfo> crossOreStationMap = OreWarMgr.getCrossOreStationMap(activityInfo.getActivityId(),groupId);
            for(CrossOreStationInfo crossOreStationInfo : crossOreStationMap.values()){
                OreWarActivityProto.OreWarActivityPitTemp.Builder temp = OreWarActivityProto.OreWarActivityPitTemp.newBuilder();
                temp.setId(crossOreStationInfo.getStationId());
                temp.setName(crossOreStationInfo.getStationName());
                temp.setIcon(crossOreStationInfo.getIcon());
                temp.setDescNum(crossOreStationInfo.getShowStationIndex());
                temp.setPos(crossOreStationInfo.getPosition());
                temp.setCapacity(crossOreStationInfo.getPitCount());
                temp.setOutputNum(crossOreStationInfo.getOutputSpeedAddition());
                temp.setOutNumAdd(crossOreStationInfo.getAddOutputSpeedAddition());
                temp.setIsNotice(crossOreStationInfo.isNotice());
                resultMsg.addPitList(temp);
            }
        }

        return resultMsg;
    }

    public static OreWarActivityProto.OreWarActivityDetailMsg.Builder parseOreWarActivityDetailMsg(long userId,Map<Integer,Map<Integer, OreWarActivityData>> activityDataMap,
                                                                                                   List<ActivityInfo> activityInfoList,
                                                                                                   Map<Integer,Map<Integer, List<ActivityConditionInfo>>> activityConditionInfoMap,
                                                                                                   Map<Integer,Map<Integer, MallInfo>> activityMallInfoMap, String language){
        OreWarActivityProto.OreWarActivityDetailMsg.Builder resultMsg = OreWarActivityProto.OreWarActivityDetailMsg.newBuilder();
        for(ActivityInfo activityInfo : activityInfoList){
            OreWarActivityProto.OreWarActivityMsg.Builder activityMsg =
                    parseOreWarActivityMsg(userId,activityInfo, activityConditionInfoMap.get(activityInfo.getActivityId()), activityMallInfoMap.get(activityInfo.getActivityId()), language);
            if (null != activityMsg) {
                resultMsg.addActivityInfo(activityMsg);
            }
        }

        //活动数据
        CommonActivityProto.CommonActivityDataMsg.Builder activityDataMsg = parseCommonActivityDataMsg(activityDataMap);
        if(null != activityDataMsg){
            resultMsg.setActivityData(activityDataMsg);
        }

        return resultMsg;
    }

    public static CommonActivityProto.CommonActivityDataMsg.Builder parseCommonActivityDataMsg(Map<Integer, Map<Integer, OreWarActivityData>> activityDataMap){
        if(null == activityDataMap){
            return null;
        }
        CommonActivityProto.CommonActivityDataMsg.Builder msg = CommonActivityProto.CommonActivityDataMsg.newBuilder();
        for (Map<Integer, OreWarActivityData> tempMap : activityDataMap.values()) {
            for (OreWarActivityData activityData : tempMap.values()) {
                for (UserActivityConditionData conditionData : activityData.getUserActivityConditionDataList()) {
                    CommonActivityProto.CommonActivityConditionDataMsg.Builder conditionDataMsg = CommonActivityPb.parseCommonActivityConditionDataMsg(conditionData);
                    if (null != conditionDataMsg) {
                        msg.addConditionDataList(conditionDataMsg);
                    }
                }
            }
        }
        return msg;
    }

/*
    public static CommonActivityProto.CommonActivityDataMsg.Builder parseCommonActivityDataMsg(Map<Integer, OreWarActivityData> activityDataMap){
        if(null == activityDataMap){
            return null;
        }
        CommonActivityProto.CommonActivityDataMsg.Builder msg = CommonActivityProto.CommonActivityDataMsg.newBuilder();
        for (OreWarActivityData activityData : activityDataMap.values()) {
            for (UserActivityConditionData conditionData : activityData.getUserActivityConditionDataList()) {
                CommonActivityProto.CommonActivityConditionDataMsg.Builder conditionDataMsg = CommonActivityPb.parseCommonActivityConditionDataMsg(conditionData);
                if (null != conditionDataMsg) {
                    msg.addConditionDataList(conditionDataMsg);
                }
            }
        }
        return msg;
    }
*/

    public static CommonActivityProto.CommonActivityDataMsg.Builder parseCommonActivityDataMsg(OreWarActivityData activityData){
        if(null == activityData){
            return null;
        }
        CommonActivityProto.CommonActivityDataMsg.Builder msg = CommonActivityProto.CommonActivityDataMsg.newBuilder();
        for (UserActivityConditionData conditionData : activityData.getUserActivityConditionDataList()) {
            CommonActivityProto.CommonActivityConditionDataMsg.Builder conditionDataMsg = CommonActivityPb.parseCommonActivityConditionDataMsg(conditionData);
            if (null != conditionDataMsg) {
                msg.addConditionDataList(conditionDataMsg);
            }
        }
        return msg;
    }
}
